package cn.lingyangwl.agile.system.service.impl;

import cn.lingyangwl.agile.model.*;
import cn.lingyangwl.agile.system.mapper.*;
import cn.lingyangwl.agile.system.model.entity.*;
import cn.lingyangwl.agile.system.model.rqrs.notice.*;
import cn.lingyangwl.agile.system.service.*;
import cn.lingyangwl.framework.mybatis.plus.utils.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shenguangyang
 */
@Service
public class SysNoticeServiceImpl implements SysNoticeService {
    @Resource
    private SysNoticeMapper noticeMapper;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice getById(Long noticeId) {
        return noticeMapper.selectById(noticeId);
    }

    @Override
    public IPage<SysNotice> pageNotice(NoticeQueryReq req) {
        LambdaQueryWrapper<SysNotice> lqw = getLqw(req);
        IPage<SysNotice> pageRq = PageUtil.to(req);
        return noticeMapper.selectPage(pageRq, lqw);
    }

    /**
     * 查询公告列表
     *
     * @param req 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> list(NoticeQueryReq req) {
        return noticeMapper.selectList(getLqw(req));
    }

    private LambdaQueryWrapper<SysNotice> getLqw(NoticeQueryReq req) {
        return SysNotice.lqw()
                .like(StringUtils.isNotEmpty(req.getNoticeTitle()), SysNotice::getNoticeTitle, req.getNoticeTitle())
                .eq(StringUtils.isNotEmpty(req.getNoticeType()), SysNotice::getNoticeType, req.getNoticeType())
                .like(StringUtils.isNotEmpty(req.getCreatedBy()), BasePlusPO::getCreatedBy, req.getCreatedBy());
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public boolean saveNotice(SysNotice notice) {
        return noticeMapper.insert(notice) > 0;
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public boolean updateNotice(SysNotice notice) {
        return noticeMapper.updateById(notice) > 0;
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public boolean deleteNotice(Long noticeId) {
        return noticeMapper.deleteById(noticeId) > 0;
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public boolean deleteBatchNotice(Long[] noticeIds) {
        return noticeMapper.deleteBatchIds(Arrays.stream(noticeIds).collect(Collectors.toList())) > 0;
    }
}
